package fr.mywiki.view.web.struts.form.admin.structure;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.ejb.FinderException;
import javax.servlet.http.HttpServletRequest;

import fr.mywiki.business.account.AccountLite;
import fr.mywiki.business.resources.ResourceManager;
import fr.mywiki.business.structure.StructureLite;
import fr.mywiki.business.structure.StructureManager;
import fr.mywiki.business.structure.TreeManager;
import fr.mywiki.model.ejbAdapter.DataController;
import fr.mywiki.view.array.ArrayController;
import fr.mywiki.view.array.ArrayDefinition;
import fr.mywiki.view.web.struts.form.ListForm;
import fr.mywiki.view.web.struts.form.structure.TreeStructureForm;

/**
 * Action form associated to the structure management.
 * 
 * @author Thomas
 */
public class ManageStructureForm extends TreeStructureForm implements ListForm {
	private static final long serialVersionUID = 1L;
	
	public static final String CONSULTING_MODE = "consulting";
	public static final String CREATING_MODE = "creating";
	public static final String EDITING_MODE = "editing";
	
	private static final String MODIFICATION_SENTENCE_KEY = "admin.structure.modification";
	private static final String CREATION_SENTENCE_KEY = "admin.structure.creation";
	
	/** Current mode of this formular : consultation, edition, creation */
	private String currentMode;
	
	/** The name of the structure the user is currently editing/creating */
	private String structName;
	
	/** The term to display for the structure root */
	private String displayedRootName;
	
	/** Do we display the elements list */
	private boolean displayElementsList;
	
	/** The introduction sentence to display when creating or modifying a structure */
	private String introSentence;
	
	/** The locale of the session */
	private Locale locale;
	
	/**
	 * The <code>ArrayController</code> dealing with the display of the accounts
	 * of a structure element.
	 */
	private ArrayController accountsController;
	
	/** The account selected by the user */
	private String idAccount;
	
	public void reloadForm(HttpServletRequest a_request) throws Exception {
		getSelectStructureController().setStructure((StructureLite) DataController.getValue(getSelectStructureController().getIdStructure(),
				StructureLite.TYPE_ENT));
		
		if ( getSelectStructureController().getStructure() != null ) {
			getSelectStructureController().setExpandJS(TreeManager.getExpandCodeJS(getSelectStructureController().getStructure()));
		} else {
			getSelectStructureController().setExpandJS(TreeManager.getExpandCodeJS(null));
			return;
		}
	}
	
	/** Initializes the formular in consulting mode */
	public void initConsultation() {
		setCurrentMode(CONSULTING_MODE);
		setDisplayElementsList(false);
	}
	
	/** Initializes the formular in modification mode */
	public void initModification() {
		setCurrentMode(EDITING_MODE);
		setDisplayElementsList(false);
		structName = getSelectStructureController().getStructure().getName();
		introSentence = ResourceManager.getMessage(MODIFICATION_SENTENCE_KEY,
				locale);
	}

	/** Initializes the formular in creation mode */
	public void initCreation() {
		setCurrentMode(CREATING_MODE);
		setDisplayElementsList(false);
		structName = new String();
		introSentence = ResourceManager.getMessage(CREATION_SENTENCE_KEY, locale);
	}
	
	/** Initializes the formular for the manipulation of the elements list */
	public void initManageElements() throws RemoteException, FinderException {
		setDisplayElementsList(true);

		List accounts = new ArrayList(StructureManager
				.getAccounts(getSelectStructureController().getStructure()));

		setAccountsController(new ArrayController(ArrayDefinition
				.getArrayDefinition(AccountLite.TYPE_ENT), accounts));
	}
	
	/**
	 * @see TreeStructureForm.centerTree()
	 */
	public void centerTree(StructureLite structure) throws RemoteException, FinderException {
		getSelectStructureController().centerTree(structure);
		if (structure == null)
			setStructName(displayedRootName);
		else
			setStructName(structure.getName());
	}

	public String getCurrentMode() {
		return currentMode;
	}

	public void setCurrentMode(String currentMode) {
		this.currentMode = currentMode;
	}

	public String getStructName() {
		return structName;
	}

	public void setStructName(String structName) {
		this.structName = structName;
	}

	public boolean isDisplayElementsList() {
		return displayElementsList;
	}

	public void setDisplayElementsList(boolean a_displayElementsList) {
		displayElementsList = a_displayElementsList;
	}

	public String getServletName() {
		return "manageStructure.do";
	}

	public String getAdditionalCodeRoot() {
		return "updateSelect(\"true\"); var objList = document.getElementById(\"listContainer\");if(objList!=null) objList.style.display=\"none\";";
	}

	public String getAdditionalCode() {
		return "updateSelect(\"false\"); var objList = document.getElementById(\"listContainer\");if(objList!=null) objList.style.display=\"none\";";
	}

	public String getDisplayedRootName() {
		return displayedRootName;
	}

	public void setDisplayedRootName(String displayedRootName) {
		this.displayedRootName = displayedRootName;
	}
	
	public Long getIdStructure() {
		return getSelectStructureController().getIdStructure();
	}

	public void setIdStructure(Long idStructure) {
		try {
			this.getSelectStructureController().setIdStructure(idStructure);
		} catch (RemoteException e) {}
	}

	public String getIntroSentence() {
		return introSentence;
	}

	public void setLocale(Locale a_locale) {
		locale = a_locale;
	}

	public ArrayController getAccountsController() {
		return accountsController;
	}

	public void setAccountsController(ArrayController accounts) {
		this.accountsController = accounts;
	}

	public String getIdElement() {
		return idAccount;
	}

	public void setIdElement(String idElement) {
		this.idAccount=idElement;
	}
}
